package com.example.recyclerview01.day04;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.example.recyclerview01.R;
import com.example.recyclerview01.baseuse.RecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 基于头部+底部控件，RecyclerView支持下拉刷新和上拉加载更多。
 */
public class PullRefreshActivity extends AppCompatActivity
        implements RefreshRecyclerView.OnRefreshListener,
        LoadRefreshRecyclerView.OnLoadMoreListener {
    private static final String TAG = "PullRefreshActivity";
    private LoadRefreshRecyclerView mRecyclerView;
    RecyclerAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_refresh);
        initData();

        mRecyclerView = findViewById(R.id.recycler_view);
        // 添加头部和底部的刷新效果。正在加载数据页面和无数据页面。
        mRecyclerView.addRefreshViewCreator(new DefaultRefreshCreator()); // 下拉刷新view
        mRecyclerView.addLoadViewCreator(new DefaultLoadCreator()); // 上拉加载更多内容...
        mRecyclerView.setOnRefreshListener(this);
        mRecyclerView.setOnLoadMoreListener(this);
        mRecyclerView.addLoadingView(findViewById(R.id.load_view)); // 加载中view
        mRecyclerView.addEmptyView(findViewById(R.id.empty_view)); // 无数据的view

        GridLayoutManager mLayoutManager = new GridLayoutManager(this, 4);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mListAdapter = new RecyclerAdapter(this, mData);
        mRecyclerView.setAdapter(mListAdapter);

        // 添加header。
        View header = LayoutInflater.from(this).inflate(R.layout.layout_header_footer, mRecyclerView, false);
        mRecyclerView.addHeaderView(header);
        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.removeHeaderView(header);
            }
        });
    }

    private List<String> mData = new ArrayList<>(); // 数据源

    protected void initData() {
        mData = new ArrayList<String>();
        for (int i = 'A'; i <= 'z'; i++) {
            mData.add("" + (char) i);
        }
        Log.d("TAG", "length:" + mData.size());
    }

    @Override
    public void onRefresh() {
        Log.d(TAG, "onRefresh");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mData.add(0, "onRefresh---");
                mListAdapter.notifyDataSetChanged();
                mRecyclerView.onStopRefresh();
            }
        }, 2000);
    }

    @Override
    public void onLoad() {
        Log.d(TAG, "onLoadMore");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mData.add("onLoadMore---");
                mListAdapter.notifyDataSetChanged();
                mRecyclerView.onStopLoad();
            }
        }, 2000);
    }
}